import ORDER from "../types/order-status";
import PAY from "../types/pay-type";
import { mapState } from "vuex";
export default {
  data() {
    return {
      ORDER,
      PAY,
    };
  },
  computed: {
    ...mapState("user", ["isLogin", "userInfo"]),
  },
  filters: {
    // 把手机号5-8位替换为*
    formatAccount(account) {
      return String(account).replace(/(\d{4})\d{3}(\d{4})/, "$1****$2");
    },
    formatType(type) {
      //0默认，1空腹，2睡前，3正餐前，4正餐后
      switch (String(type)) {
        case "1":
          return "起床空腹";
        case "2":
          return "睡前";
        case "3":
          return "正餐前";
        case "4":
          return "正餐后";
        case "0":
        default:
          return "随记";
      }
    },
    formatSex(value) {
      switch (String(value)) {
        case "0":
          return "男";
        case "1":
          return "女";
        default:
          return "去设置";
      }
    },
  },
  methods: {
    async pay({ orderId, cb }) {
      try {
        const { data } = await this.$ajax.order.pay({
          userId: this.userInfo.userId,
          orderId,
        });
        // 把res.data对象内的所有key转换为小写，并赋值给orderInfo
        const orderInfo = { signType: "MD5" };
        Object.assign(orderInfo, data);
        console.log("获取到支付前的参数", orderInfo);
        uni.requestPayment({
          provider: "wxpay",
          ...orderInfo,
          success: function (res2) {
            console.log("获取到支付后的参数", res2);
            cb && cb();
          },
          fail: function (err) {
            console.log("fail:" + JSON.stringify(err));
          },
        });
      } catch (error) {}
    },
    $showModal(error) {
      this.$modal({
        content: error.message || error.errmsg || "系统繁忙，请稍后再试",
      });
    },
    formatMobile(phone) {
      return String(phone).replace(/(\d{3})\d{4}(\d{4})/, "$1****$2");
    },
  },
};
